package com.tuanzi.loan.business.service.user.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.constants.user.LoanUserAgreementStatus;
import com.tuanzi.loan.business.entity.product.product.LoanProductInfo;
import com.tuanzi.loan.business.entity.system.Dictionary;
import com.tuanzi.loan.business.entity.user.LoanUserAgreement;
import com.tuanzi.loan.business.entity.user.LoanUserAgreementOnlineProduct;
import com.tuanzi.loan.business.repository.user.LoanUserAgreementOnlineProductRepository;
import com.tuanzi.loan.business.repository.user.LoanUserAgreementRepository;
import com.tuanzi.loan.business.repository.user.custom.LoanUserAgreementCustomRepository;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.business.service.user.LoanUserAgreementService;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.user.LoanUserAgreementSearchRequest;
import com.tuanzi.loan.vo.response.user.LoanUserAgreementResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chenweijun
 * @Date: 2019/9/25 10:44
 */
@Service
public class LoanUserAgreementServiceImpl extends BaseAuditServiceImpl<LoanUserAgreement, Long> implements LoanUserAgreementService {

    @Autowired
    private DictionaryService dictionaryService;

    @Override
    protected JPAAuditRepository<LoanUserAgreement, Long> getRepository() {
        return loanUserAgreementRepository;
    }

    @Autowired
    private LoanUserAgreementCustomRepository loanUserAgreementCustomRepository;
    @Autowired
    private LoanUserAgreementRepository loanUserAgreementRepository;
    @Autowired
    private LoanUserAgreementOnlineProductRepository loanUserAgreementOnlineProductRepository;


    @Override
    public Page<LoanUserAgreementResponse> list(PageSearcher<LoanUserAgreementSearchRequest> pageSearcher) {

        List<Dictionary> dictionaries = dictionaryService.findAllByCode(DictionaryCode.LARGE_COMMON_PRODUCT);

        Page<LoanUserAgreementResponse> responses = loanUserAgreementCustomRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
        responses.forEach(x ->{
            List<LoanUserAgreementOnlineProduct> loanUserAgreementOnlineProducts = loanUserAgreementOnlineProductRepository.findAllByRuleId(x.getRuleId());
            List<String> parentProductIds = loanUserAgreementOnlineProducts.stream().map(LoanUserAgreementOnlineProduct::getParentProductId).collect(Collectors.toList());

            List<String> parentProductNames = Lists.newArrayList();
            parentProductIds.forEach(parentProductId ->{
               dictionaries.stream().forEach(dictionary -> {
                   if(parentProductId.equals(dictionary.getValue1())){
                       parentProductNames.add(dictionary.getName());
                   }
               });
            });
            x.setParentProductId(StringUtils.join(parentProductNames, "、"));
        });

        return responses;
    }

    @Override
    public LoanUserAgreement findOneByRuleId(Integer ruleId) {
        return loanUserAgreementRepository.findByRuleId(ruleId);
    }


    @Override
    public void online(Long id) {
        LoanUserAgreement loanUserAgreement = loanUserAgreementRepository.findOne(id);
        loanUserAgreement.setStatus(LoanUserAgreementStatus.ONLINE.getStatus());
        saveOrUpdate(loanUserAgreement);
    }

    @Override
    public void offline(Long id) {
        LoanUserAgreement loanUserAgreement = loanUserAgreementRepository.findOne(id);
        loanUserAgreement.setStatus(LoanUserAgreementStatus.OFFLINE.getStatus());
        saveOrUpdate(loanUserAgreement);
    }

    @Override
    public void delete(Long id) {
        LoanUserAgreement loanUserAgreement =  loanUserAgreementRepository.findOne(id);
        loanUserAgreementRepository.delete(id);
        loanUserAgreementOnlineProductRepository.deleteByRuleId(loanUserAgreement.getRuleId());
    }

    @Override
    public void save(LoanUserAgreement loanUserAgreement, List<String> parentProductIds) {

        loanUserAgreement.setStatus(LoanUserAgreementStatus.OFFLINE.getStatus());
        loanUserAgreement.setCreateDate(new Date());
        loanUserAgreementRepository.save(loanUserAgreement);

        loanUserAgreementOnlineProductRepository.save(parentProductIds.stream().map(x->{

            LoanUserAgreementOnlineProduct loanUserAgreementOnlineProduct = new LoanUserAgreementOnlineProduct();
            loanUserAgreementOnlineProduct.setRuleId(loanUserAgreement.getRuleId());
            loanUserAgreementOnlineProduct.setParentProductId(x);
            return loanUserAgreementOnlineProduct;
        }).collect(Collectors.toList()));

    }

    @Override
    public LoanUserAgreement findTopOne() {
        return loanUserAgreementCustomRepository.findTopOne();
    }


}
